package com.hare.common.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.hare.common.exception.ServiceException;

import java.util.Map;

/**
 * @program: Hare
 * @description: 对象Copy
 * @author: Hare
 * @create: 2022-11-15 16:03
 * @Version 6.0.1
 **/
public class BeanUtils {

    /**
     * 忽略空copy
     *
     * @param source 源对象
     * @param target 目标
     * @throws ServiceException
     */
    public static void copyProperties(Object source, Object target) throws ServiceException {
        copyProperties(source, target, "id", "createdTime", "updatedTime", "isDeleted", "createdBy", "updatedBy");
    }

    public static void copyPropertiesCancelIgnore(Object source, Object target) throws ServiceException {
        BeanUtil.copyProperties(source, target,
                CopyOptions.create().setIgnoreNullValue(true));
    }

    /**
     * 忽略空copy
     *
     * @param source           源对象
     * @param target           目标
     * @param ignoreProperties
     */
    public static void copyProperties(Object source, Object target, String... ignoreProperties) {
        BeanUtil.copyProperties(source, target,
                CopyOptions.create().setIgnoreNullValue(true).setIgnoreProperties(ignoreProperties));
    }

    /**
     * obj转换为map
     *
     * @param obj
     * @return
     */
    public static Map<String, Object> beanToMap(Object obj) {
        return BeanUtil.beanToMap(obj);
    }

}
